/*
 * PreConfig.java
 * 
 * Version information
 * 
 * Apr 8, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import static net.amsoft.iservice.icontact.util.IContactConstants.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.icontact.hibernate.ContactPage;
import net.amsoft.iservice.icontact.hibernate.dataObject.LoginData;
import net.amsoft.iservice.icontact.service.IContact;
import net.amsoft.iservice.icontact.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.icontact.webapp.struts.form.LoginForm;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

/**
 * This action class is used to configure the Contact Page of the i-name.
 * 
 * @author root
 * 
 */
public class PreConfig extends BaseAction {
	private static final Logger oLogger = Logger.getLogger(PreConfig.class);

	@Override
	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(SESSION_LOGIN_OBJ);
		if (oLoginData.isLoginStatus() == false) {
			return oMapping.findForward(FWD_CHKLOGIN);
		}
		String sSynonym = ((LoginForm) oForm).getSynonym();
		if (sSynonym == null || sSynonym.trim().length() == 0
				|| !oLoginData.getAllSynonyms().contains(sSynonym)) {
			oLogger.debug("process() : Synonym=" + sSynonym);
			sSynonym = oLoginData.getLoggedInSynonym();
		}
		ContactPage oContactPage = null;
		ConfigForm oConfigForm = new ConfigForm();
		try {
			oContactPage = IContact.getContactPage(sSynonym);
			oContactPage.update();
			oConfigForm.setType(TYPE_ACTIVATED_SEPARATE);
			oConfigForm.setDisplayMsg(oContactPage.getDisplayMsg());
			oConfigForm.setDisplayName(oContactPage.getDisplayName());
			oConfigForm.setNotificationEmailMsg(oContactPage
					.getNotificationEmailMsg());
			oConfigForm.setNotificationEmailSub(oContactPage
					.getNotificationEmailSub());
			oConfigForm.setTags(oContactPage.getTags());
			oConfigForm.setVerificationEmailMsg(oContactPage
					.getVerificationEmailMsg());
			oConfigForm.setVerificationEmailSub(oContactPage
					.getVerificationEmailSub());
			oConfigForm.setVerificationType(String.valueOf(oContactPage
					.getVerificationType()));
			try {
				String sEmailAddr = new String();

				oContactPage.getEmail();
				for (Iterator itr = oContactPage.getEmail().iterator(); itr
						.hasNext();) {
					sEmailAddr += "\r\n" + (String) itr.next();
				}
				oConfigForm.setEmailAddresses(sEmailAddr);
			} catch (Exception e) {
				oLogger.error("process() : No notification email addresses");
			}
		} catch (Exception e) {
			oConfigForm.setType(TYPE_DEACTIVATED);
			oLogger
					.error("process() : Exception : No contact page exist for Synonym:"
							+ sSynonym);
		}
		oConfigForm.setSynonym(sSynonym);
		ArrayList<LabelValueBean> synonymsGroups = new ArrayList<LabelValueBean>();

		HashMap groupMap = IContact.getSynonymGroups(oLoginData
				.getAllSynonyms());
		Set set = groupMap.keySet();
		Iterator iter = set.iterator();
		while (iter.hasNext()) {
			ArrayList group = (ArrayList) groupMap.get(iter.next());
			if (group.contains(sSynonym)) {
				if (group.size() > 1) {
					String allSyns = "";
					oConfigForm.setType(TYPE_GROUP_ACTIVATED);
					String sKey = (String) group.get(0);
					for (int i = 0; i < group.size(); i++) {
						if (!group.get(i).equals(sSynonym)) {
							allSyns += group.get(i) + ",";
							sKey = (String) group.get(i);
						}
					}
					oConfigForm.setSameAsSynonyms(sKey);
					LabelValueBean labelValueBean = new LabelValueBean(sKey,
							allSyns.substring(0, allSyns.length() - 1));
					synonymsGroups.add(labelValueBean);
				}
			} else {
				String allSyns = "";
				for (int i = 0; i < group.size(); i++) {
					allSyns += group.get(i) + ",";
				}
				LabelValueBean labelValueBean = new LabelValueBean(
						(String) group.get(0), allSyns.substring(0, allSyns
								.length() - 1));
				synonymsGroups.add(labelValueBean);
			}
		}

		oRequest.setAttribute(REQ_GRP_SYNONYMS, synonymsGroups);
		oRequest.setAttribute(SESSION_CONFIG_FRM, oConfigForm);
		oLogger.info("process() : Forward to configure page");
		return oMapping.findForward(FWD_SUCCESS);
	}
}
